﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Xml;
using System.Xml.Serialization;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.IO;
using System.Xml.XPath;


namespace Avatar
{
    public static class Example
    {
        public static XmlDocument RSSResolver()
        {
            string fullfeed = @"http://www.douban.com/feed/people/{0}/miniblogs";
            string id = "frankel";
            Uri uri = new Uri(string.Format(fullfeed, id));

            WebClient client = null;
            string fulltext = string.Empty;
            try
            {
                client = new WebClient();
                client.Encoding = Encoding.UTF8;
                fulltext = client.DownloadString(uri);
            }
            catch
            {
                throw;
            }
            finally
            {
                client.Dispose();
            }
                
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(fulltext);

            string contect = doc.GetPrefixOfNamespace("http://purl.org/rss/1.0/modules/contect/");
            string dc = doc.GetPrefixOfNamespace("http://purl.org/dc/elements/1.1");
            XmlNodeList nodes = doc.SelectNodes("//item");
            XmlSerializer s = new XmlSerializer(typeof(RSS)); //RSS=>Item
            MemoryStream ms = null;
            try
            {
                ms = new MemoryStream();
                byte[] b = Encoding.UTF8.GetBytes(fulltext); //fulltext -> RSS nodes[i].OuterXml -> Item
                ms.Write(b, 0, b.Length);
                ms.Position = 0;
                RSS rss = s.Deserialize(ms) as RSS;//RSS=>Item
            }
            catch
            {
                throw;
            }
            finally
            {
                ms.Close();
            }
            return doc;
            

        }

        public static XPathDocument APIResolver()
        {
            string fullfeed = @"http://api.douban.com/people/{0}";
            string id = "ahbei";
            Uri uri = new Uri(string.Format(fullfeed, id));
            
            XPathNavigator nav = null;
            XPathDocument docnav = new XPathDocument(uri.ToString());
            nav = docnav.CreateNavigator();

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
            nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");
            nsmgr.AddNamespace("db", "http://www.douban.com/xmlns/");
            nsmgr.AddNamespace("gd", "http://schemas.google.com/g/2005");
            nsmgr.AddNamespace("opensearch", "http://a9.com/-/spec/opensearchrss/1.0/");
            
            nav.MoveToRoot();
            XPathNavigator nid = nav.SelectSingleNode("/atom:entry/atom:id", nsmgr);
            XPathNavigator ntitle = nav.SelectSingleNode("/atom:entry/atom:title", nsmgr);
            XPathNavigator ncontent = nav.SelectSingleNode("/atom:entry/atom:content", nsmgr);
            XPathNavigator nuri = nav.SelectSingleNode("/atom:entry/atom:uri", nsmgr);

            do
            {
                switch (nav.NodeType)
                {
                    case XPathNodeType.Element:;
                    break;
                    case XPathNodeType.Attribute:;
                    break;
                    default:
                    break;
                }

            }
            while (nav.MoveToNext());


            return docnav;
        }

        public static IEnumerable<string> GetCurrentItemEmail()
        {
            //sample code for how to get the default folder without using activeexplorer()
            //Outlook.NameSpace ReadOutlookNameSpace = Globals.ThisAddIn.Application.GetNamespace("MAPI");
            //Outlook.MAPIFolder DefalutContactFolder = ReadOutlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);

            //get the current inspector is key point.
            Outlook.Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();
            List<string> emails = new List<string>();
            if (inspector.CurrentItem is Outlook.ContactItem)
            {
                Outlook.ContactItem citem = inspector.CurrentItem as Outlook.ContactItem;
                emails.AddRange(new string[]{ citem.Email1Address, citem.Email2Address, citem.Email3Address});
            }
            else if (inspector.CurrentItem is Outlook.ExchangeUser)
            {
                Outlook.ExchangeUser eitem = inspector.CurrentItem as Outlook.ExchangeUser;
                emails.Add(eitem.PrimarySmtpAddress);
            }
            else if (inspector.CurrentItem is Outlook.MailItem)
            {
                Outlook.MailItem mitem = inspector.CurrentItem as Outlook.MailItem;
                switch (mitem.Sender.AddressEntryUserType)
                {
                    case Outlook.OlAddressEntryUserType.olOutlookContactAddressEntry:
                        Outlook.ContactItem citem = mitem.Sender.GetContact();
                        if (citem != null)
                        {
                            emails.AddRange(new string[] { citem.Email1Address, citem.Email2Address, citem.Email3Address });
                        }
                        break;
                    case Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry:
                        Outlook.ExchangeUser eitem = mitem.Sender.GetExchangeUser();
                        if (eitem != null)
                        {
                            emails.Add(eitem.PrimarySmtpAddress);
                        }
                        break;
                    default:
                        break;
                }                
            }

            return (IEnumerable<string>)emails;
        }

    }

    [XmlRoot(ElementName="rss")]
    public class RSS
    {
        [XmlAttribute("version")]
        public string Version;
        [XmlElement("channel")]
        public Channel Channel;
    }

    public class Channel
    {
        [XmlElement("title")]
        public string Title;
        [XmlElement("link")]
        public string Link;
        [XmlElement("description")]
        public string Description;
        [XmlElement("language")]
        public string Language;
        [XmlElement("copyright")]
        public string Copyright;
        [XmlElement("pubDate")]
        public string PubDate;
        [XmlArrayItem("item", IsNullable=true)]
        public Item[] Item;
    }

    public class Item
    {
        [XmlElement("title")]
        public string Title;
        [XmlElement("link")]
        public string Link;
        [XmlElement("description")]
        public string Description;
        [XmlElement("creator", Namespace="http://purl.org/dc/elements/1.1/")]
        public string Creator;
        [XmlElement("pubDate")]
        public string PubDate;
        [XmlElement("guid")]
        public GUID GUID;
    }

    public class GUID
    {
        [XmlAttribute("isPermeLink")]
        public bool IsPermeLink;
        [XmlText]
        public string uri;  
    }


}
